# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
@Author : Ayao
@Time   : 2022/5/9
@File   : 最长公共前缀.py
"""
"""
编写一个函数来查找字符串数组中的最长公共前缀。

如果不存在公共前缀，返回空字符串 ""。

 

示例 1：

输入：strs = ["flower","flow","flight"]
输出："fl"
示例 2：

输入：strs = ["dog","racecar","car"]
输出：""
解释：输入不存在公共前缀。
 

提示：

1 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i] 仅由小写英文字母组成
相关标签
字符串

作者：力扣 (LeetCode)
链接：https://leetcode.cn/leetbook/read/array-and-string/ceda1/
来源：力扣（LeetCode）
著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
"""


#
class Solution:
    def longestCommonPrefix(self, strs) -> str:
        if len(strs) == 0:
            return ''
        elif len(strs) == 1:
            return strs[0]
        minLen = len(strs[0])
        for st in strs:
            if len(st) <= minLen:
                minLen = len(st)
        if minLen == 0:
            return ""
        for i in range(minLen):
            prefix = strs[0][i]
            for st in strs:
                if prefix == st[i]:
                    continue
                else:
                    if i == 0:
                        return ""
                    else:
                        return strs[0][:i]
        return strs[0][:minLen]


strs = ["flower", "flow", "flight", "dog", "racecar", "car"]
print(Solution().longestCommonPrefix(["flower", "flow", "flight"]))
print(Solution().longestCommonPrefix(["dog", "racecar", "car"]))
print(Solution().longestCommonPrefix(["radraog", "racecar", "racar"]))
print(Solution().longestCommonPrefix([""]))
print(Solution().longestCommonPrefix(["a"]))
print(Solution().longestCommonPrefix(["ab", "a"]))

# minlen = len(strs[0])
# for st in strs:
#     if len(st) < minlen:
#         minlen = len(st)
#
# print(minlen)
